package approve.adapter;

import model.pojo.Employee;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import approve.action.R;

public class EApproveAdapter extends ArrayAdapter<String>{
	Context context;
	String[] values;
	Bundle language;
	String database,lang="EN";
	Employee emp= new Employee();
	int[] countarray;
	
	RelativeLayout flag = null;
	ImageView imageMenuLeft = null;
	ImageView imageMenuRight = null;

	public Bundle getLanguage() {
		return language;
	}

	public void setLanguage(Bundle language) {
		this.language = language;
	}
	
	public EApproveAdapter(Context context,String[] values,Bundle language,Employee emp,String database,String lang,int[] countarray){
		super(context, R.layout.adapter_list_approval,values);
		this.context = context;
		this.values = values;
		this.language = language;
		this.emp = emp;
		this.database = database;
		this.lang = lang;
		this.countarray=countarray;
	}

	@Override
	public View getView(int position, View convertview, ViewGroup parent) {
		
			LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertview=inflater.inflate(R.layout.adapter_list_approvalmain, null);
			TextView textView = (TextView) convertview.findViewById(R.id.labelListApp);
			ImageView imageView = (ImageView) convertview.findViewById(R.id.logo);
			
//			final TableRow tableRow1 = (TableRow)convertview.findViewById(R.id.tableRow1);
			final RelativeLayout relative1 = (RelativeLayout)convertview.findViewById(R.id.relativeMain);
			TextView lbReceiveDoc = (TextView)convertview.findViewById(R.id.lbReceiveDocument);
			ImageView imageReceiDoc = (ImageView)convertview.findViewById(R.id.imageReceiveDocument);
			
			
			final RelativeLayout relative2 = (RelativeLayout)convertview.findViewById(R.id.relative2);
			final RelativeLayout relativeMenu = (RelativeLayout)convertview.findViewById(R.id.relativeMenu);
			final RelativeLayout relativeMainMenu = (RelativeLayout)convertview.findViewById(R.id.relativeMainMenu);
			
			Resources res = context.getResources();
	        final BitmapDrawable background = (BitmapDrawable) res.getDrawable(R.drawable.repeat_main);
	        background.setTileModeX(Shader.TileMode.REPEAT);
	        background.setTileModeY(Shader.TileMode.REPEAT);
//	        tableRow1.setBackgroundDrawable(background);
	        relativeMainMenu.setBackgroundDrawable(background);		
	        
	        
	        Resources res1 = context.getResources();
	        final BitmapDrawable background1 = (BitmapDrawable) res1.getDrawable(R.drawable.repeat_menu);
	        background1.setTileModeX(Shader.TileMode.REPEAT);
	        background1.setTileModeY(Shader.TileMode.REPEAT);
	        relative2.setBackgroundDrawable(background1);
	        
	        final ImageView imageViewMenuLeft = (ImageView)convertview.findViewById(R.id.imageViewMenuLeft);
			final ImageView imageViewMenuRight = (ImageView)convertview.findViewById(R.id.imageViewMenuRight);
			
			 if(position == 0){
					lbReceiveDoc.setText(language.getString("privateMemo"));
					imageReceiDoc.setImageResource(R.drawable.private_memo);
					
					relativeMenu.setVisibility(View.GONE);
					relative1.setOnClickListener(null);
			 }
			 else if(position==2){
				lbReceiveDoc.setText(language.getString("received-document"));
				imageReceiDoc.setImageResource(R.drawable.received_document);

				relativeMenu.setVisibility(View.GONE);
				
				relative1.setOnClickListener(null);
			}else if(position==10){
				lbReceiveDoc.setText(language.getString("my-document"));
				imageReceiDoc.setImageResource(R.drawable.icon7);

				relativeMenu.setVisibility(View.GONE);
				relative1.setOnClickListener(null);
			}else if(position == 16){
				lbReceiveDoc.setText(language.getString("recycle_bin"));
				imageReceiDoc.setImageResource(R.drawable.recycle);
				
				relativeMenu.setVisibility(View.GONE);
				relative1.setOnClickListener(null);
			}else if(position == 19){
				lbReceiveDoc.setText(language.getString("library_document"));
				imageReceiDoc.setImageResource(R.drawable.library);
				relative1.setOnClickListener(null);
				
				relativeMenu.setVisibility(View.GONE);
			}
			else{
				relative1.setVisibility(View.GONE);
				
				textView.setText(values[position]);	
				String item = values[position];
		 
				if (item.equals(language.getString("pending-document"))) {
					imageView.setImageResource(R.drawable.icon2);
					int count=countarray[0];
					textView.setText(values[position]+ " " + "(" + count +")");
				}else if(item.equals(language.getString("not-approved-document"))){
					imageView.setImageResource(R.drawable.icon3);
					int count=countarray[1];
					textView.setText(values[position]+ " " + "(" + count +")");
				}else if(item.equals(language.getString("document_approval"))){
					imageView.setImageResource(R.drawable.icon4);
					int count=countarray[2];
					textView.setText(values[position]+ " " + "(" + count +")");
				}else if(item.equals(language.getString("docDecline"))){
					imageView.setImageResource(R.drawable.icon5);
					int count=countarray[3];
					textView.setText(values[position]+ " " + "(" + count +")");
				}else if(item.equals(language.getString("reference_document")+"-"+language.getString("empApprove"))){
					imageView.setImageResource(R.drawable.icon13);
					int count=countarray[4];
					textView.setText(values[position]+  " " + "(" + count +")");
				}else if(item.equals(language.getString("reference_document")+"-"+language.getString("empNotApprove"))){
					imageView.setImageResource(R.drawable.icon14);
					int count=countarray[5];
					textView.setText(values[position]+  " " + "(" + count +")");
				}else if(item.equals(language.getString("metting_pending"))){
					imageView.setImageResource(R.drawable.icon6);
					int count=countarray[6];
					textView.setText(values[position]+  " " + "(" + count +")");
				}else if(item.equals(language.getString("mydocsending"))){
					imageView.setImageResource(R.drawable.icon2);
					int count=countarray[7];
					textView.setText(values[position]+  " " + "(" + count +")");
				}else if(item.equals(language.getString("mydocsave"))){
					imageView.setImageResource(R.drawable.icon9);
					int count=countarray[8];
					textView.setText(values[position]+  " " + "(" + count +")");
				}else if(item.equals(language.getString("mydocappro"))){
					imageView.setImageResource(R.drawable.icon4);
					int count=countarray[9];
					textView.setText(values[position]+  " " + "(" + count +")");
				}else if(item.equals(language.getString("mydeclinedoc"))){
					imageView.setImageResource(R.drawable.icon5);
					int count=countarray[10];
					textView.setText(values[position]+  " " + "(" + count +")");
				}else if(item.equals(language.getString("mymeetingdcsending"))){
					imageView.setImageResource(R.drawable.icon6);
					int count=countarray[11];
					textView.setText(values[position]+  " " + "(" + count +")");
				}else if(item.equals(language.getString("recycle_receveidoc"))){
					imageView.setImageResource(R.drawable.receive);
					int count=countarray[12];
					System.out.println("count == " + count);
					textView.setText(values[position]+  " " + "(" + count +")");
				}else if(item.equals(language.getString("recylce_sending_document"))){
					imageView.setImageResource(R.drawable.icon2);
					int count=countarray[13];
					textView.setText(values[position]+  " " + "(" + count +")");
				}
				else if(item.equals(language.getString("new_lable"))){
					imageView.setImageResource(R.drawable.label);
					textView.setText(language.getString("new_lable"));
				}
				else if(item.equals(language.getString("listprivatememo"))){
					imageView.setImageResource(R.drawable.private_memo);
					textView.setText(language.getString("listprivatememo"));
				}
			}
			
			
			OnTouchListener onTouchListener = new OnTouchListener() {
				
				@Override
				public boolean onTouch(View arg0, MotionEvent event) {
					if(flag!=null && imageMenuLeft !=null && imageMenuRight != null){
						flag.setBackgroundDrawable(background1);
						imageMenuLeft.setImageResource(R.drawable.radius_menu_left_foccused);
						imageMenuRight.setImageResource(R.drawable.radius_menu_right_foccused);
					}

					if (event.getAction() == MotionEvent.ACTION_DOWN) {
						imageViewMenuLeft.setImageResource(R.drawable.radius_main_left_pressed);
						relative2.setBackgroundResource(R.drawable.repeat_main_pressed);
						imageViewMenuRight.setImageResource(R.drawable.radius_main_right_pressed);
					} else if (event.getAction() == MotionEvent.ACTION_UP) {
						imageViewMenuLeft.setImageResource(R.drawable.radius_main_left_pressed);
						relative2.setBackgroundResource(R.drawable.repeat_main_pressed);
						imageViewMenuRight.setImageResource(R.drawable.radius_main_right_pressed);
					}else if(event.getAction() == MotionEvent.ACTION_MOVE){
						imageViewMenuLeft.setImageResource(R.drawable.radius_menu_left_foccused);
						relative2.setBackgroundResource(R.drawable.bg_menu_item);
						imageViewMenuRight.setImageResource(R.drawable.radius_menu_right_foccused);
					}
					imageMenuLeft = imageViewMenuLeft;
					flag = relative2;
					imageMenuRight = imageViewMenuRight;
					
					return false;
				}
			};
			
			relative2.setOnTouchListener(onTouchListener);
			
			
		return convertview;
	}

	public String getDatabase() {
		return database;
	}

	public void setDatabase(String database) {
		this.database = database;
	}

	public Employee getEmp() {
		return emp;
	}

	public void setEmp(Employee emp) {
		this.emp = emp;
	}
}
